// pages/confirm_order/confirm_order.js
// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    goods_id: '',
    list: '',
    address: '',
    number: 1,
    allfreight: '',
    allprice: '',
    res_null: '',

  },
  getDataBindTap: function (e) {
    var result = e.detail.value;
    console.log(result)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.goods_id);
    // console.log('222',options);
    // 详情列表
    this.setData({
      goods_id: options.goods_id
    })
    this.list(this.data.goods_id)
    this.address()
  },
  // 详情
  list(id) {
    wx.request({
      url: `${baseurl}/good.ExchangeShop/good_details`,
      method: 'POST',
      data: {
        goods_id: id,
        user_id: wx.getStorageSync("user_id"),
      },
      success: (res) => {
        // console.log(res.data.data);

        this.setData({
          list: res.data.data,
          // collect: res.data.data.collect
        })
      }
    })
  },
  add() {
    let num = this.data.number
    if (num < 999) {
      num++
      this.setData({
        number: num
      });
      this.freight()
    } else {
      wx.showToast({
        title: '库存不足',
        icon: 'none',
        duration: 2000
      })
    }
  },
  reduce() {
    let num = this.data.number
    if (num > 1) {
      num--
      this.freight()
      this.setData({
        number: num
      });

    } else {
      wx.showToast({
        title: '最少兑换1件',
        icon: 'none',
        duration: 2000
      })
    }
  },
  // 运费
  freight() {
    wx.request({
      url: `${baseurl}/good.ExchangeShop/Freight`,
      method: 'POST',
      data: {
        user_id: wx.getStorageSync("user_id"),
        province: this.data.address.province,
      },
      success: (res) => {
        // console.log(this.data.address.province, res);
        // console.log(this.data.address.province, res.data.data);
        // console.log(this.data.address.province, res.data.data == null);
        this.setData({
          res_null: res.data.data
        })
        let result = res
        // 判断不用运费的
        if (res.data.data == null) {
          // console.log('暂不配送');
          // allfreight = 0
          let price = 0 // 商品小计
          price = this.data.list.data.price * this.data.number
          wx.showToast({
            title: '地址暂不配送',
            icon: 'none',
            duration: 2000
          })

          this.setData({
            allfreight: '地址暂不配送',
            allprice: price
          })
          // console.log(this.data.allfreight);
        } else {
          // 运费
          let First_price = result.data.data.price  // 首件运费
          let lastprice = result.data.data.prices  // 多出的运费
          let sum = this.data.number  // 获取件数
          let price = 0 // 商品小计
          let allfreight = 0; // 总运费
          let freight = result.data.data  // 运费
          var weight = parseFloat(this.data.list.data.weight) * parseFloat(sum)  // 重量

          price = price + (this.data.list.data.price * this.data.number)

          // 运费改成按照每个省份的首件*1+（n-1）*多余价格
          // allfreight = First_price * 1 + (sum - 1) * lastprice
          // price = price + allfreight // 按件收费

          // 运费 首重 + (n-1) * 多余价格
          if (weight <= 0) {
            allfreight = parseFloat(First_price)
          } else {
            allfreight = parseFloat(First_price) + parseFloat((weight - 1) * lastprice)
          }

          console.log(First_price, weight, allfreight);
          allfreight = allfreight.toFixed(2)  // 按重量收费
          
          price = parseFloat(price) + parseFloat(allfreight)

          this.setData({
            allfreight: allfreight,
            allprice: price
          })
        }

      }
    })
  },
  address() {
    wx.request({
      url: `${baseurl}/user.UserAddress/get_details`,
      method: 'POST',
      data: {
        status: 1,
        user_id: wx.getStorageSync("user_id"),
      },
      success: (res) => {
        this.setData({
          address: res.data.data,
        })
        this.freight()
      }
    })
  },
  // 立即支付
  puy() {
    if (this.data.res_null == null) {
      wx.showToast({
        title: '地址不支持配送 ！',
        icon: 'none'
      })
    } else {
      wx.request({
        url: `${baseurl}/good.ExchangeShop/exchange_add`,
        method: 'POST',
        data: {
          goods_id: this.data.goods_id,
          user_id: wx.getStorageSync("user_id"),
          openid: wx.getStorageSync("openid"),
          total_price:this.data.allprice,
          province: this.data.address.province,
          city: this.data.address.city,
          area: this.data.address.area,
          address: this.data.address.address,
          user_name: this.data.address.user_name,
          phone: this.data.address.phone,
          price: this.data.list.data.price,
          number: 1,
          title: this.data.list.data.title,
          thumb: this.data.list.data.thumb,
        },
        success: (res) => {
          console.log(res.data.msg);
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          // 支付成功
          wx.reLaunch({
            url: `/pages/duihuandingdan/duihuandingdan?currentTab=1`,
          })
        }
      })
    }


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.address()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})